﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Sqlite.Interfaces;
using Company.Sqlite.Models;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations.Model;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using 无人值守地磅称重系统.Dialogs;
using 无人值守地磅称重系统.Interfaces;
using static System.Data.Entity.Infrastructure.Design.Executor;

namespace 无人值守地磅称重系统.ViewModels
{
    internal class UserViewModel : ObservableObject
    {
        public User NewUser { get; set; } = new User();

        private List<User> users;

        public List<User> Users { get => users; set => SetProperty(ref users, value); }

        public ICommand InsertCommand { get; }

        public ICommand UpdateCommand { get; }

        public ICommand DeleteCommand { get; }

        public ICommand SearchCommand { get; }

        public ICommand ClearKeywordCommand { get; }

        public ISession Session { get; }

        private IUserRepository UserRepository { get;}

        public UserViewModel(IUserRepository userRepository, ISession session)
        {
            UserRepository= userRepository;
            Session= session;

            InsertCommand = new RelayCommand(Insert);
            UpdateCommand = new RelayCommand<User>(Update);
            DeleteCommand = new RelayCommand<User>(Delete);
            SearchCommand = new RelayCommand<string>(Search);
            ClearKeywordCommand = new RelayCommand<TextBox>(ClearKeyword);

            Users = userRepository.GetAll();
        }

        private void ClearKeyword(TextBox textBox)
        {
            textBox.Text = string.Empty;
            Users = UserRepository.GetAll();
        }

       

        private void Search(string keyword)
        {
            if(string.IsNullOrEmpty(keyword))
            {
                Users = UserRepository.GetAll();
            }
            else
            {
                Users = UserRepository.GetAll(keyword);
            }
        }

        private async void Delete(User user)
        {
            if(Session.CurrentUser.Id == user.Id)
            {
                Session.Show("不能删除自己");
            }

            if(user.Id == 1)
            {
                Session.Show("不能删除超级管理员");
                return;
            }

            var view = new Dialog("是否执行删除操作？", System.Windows.MessageBoxButton.YesNo);
            bool result = (bool)await DialogHost.Show(view, "RootDialog");
            if (result)
            {
                int count = UserRepository.Delete(user);
                if(count > 0)
                {
                    Session.Show("删除成功！");
                    Users = UserRepository.GetAll();
                }
            }
        }

        private void Update(User user)
        {
            if (string.IsNullOrEmpty(user.UserName))
            {
                Session.Show("请输入用户名");
                return;
            }

            if (string.IsNullOrEmpty(user.Password))
            {
                Session.Show("请输入密码");
                return;
            }

            int count = UserRepository.Update(user);
            if(count > 0)
            {
                Session.Show("修改成功！");
                Users = UserRepository.GetAll();
            }
        }

        private void Insert()
        {
            if (string.IsNullOrEmpty(NewUser.UserName))
            {
                Session.Show("请输入用户名");
                return;
            }

            if (string.IsNullOrEmpty(NewUser.Password))
            {
                Session.Show("请输入密码");
                return;
            }
            
            if (UserRepository.Select(NewUser.UserName) != null)
            {
                Session.Show("该用户已经存在");
                return;
            }

            User user = new User();
            user.UserName = NewUser.UserName;
            user.Password = NewUser.Password;
            user.Role = NewUser.Role;
            user.InsertDate= NewUser.InsertDate;

            int count = UserRepository.Insert(user);

            if(count> 0)
            {
                Session.Show("添加成功");
                NewUser.UserName=string.Empty; 
                NewUser.Password=string.Empty;
                Users = UserRepository.GetAll();
            }
        }
    }
}
